﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ENLibraryClass;
using System.Data.SqlClient;

using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;

namespace uacars
{
    public partial class frmMenuSuperior : Form
    {
        private string auxLogin;
        private string auxPass;

        public frmMenuSuperior()
        {
            InitializeComponent();
        }

        public frmMenuSuperior(string usu, string pass)
        {
            InitializeComponent();
            auxLogin = usu;
            auxPass = pass;
        }

        private void btnCitas_Click_1(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                DataTable tmp = new DataTable();
                frmCitas Citas = new frmCitas(tmp);
                Citas.MdiParent = this.MdiParent;
                Citas.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmCitas")
                {
                    DataTable tmp = new DataTable();
                    frmCitas Citas = new frmCitas(tmp);
                    Citas.MdiParent = this.MdiParent;
                    Citas.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }

            btnCitas.Image = uacars.Properties.Resources.citasC;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = false;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void btnClientes_Click_1(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmClientes Clientes = new frmClientes(labelOcupacion.Text, auxLogin, auxPass);
                Clientes.MdiParent = this.MdiParent;
                Clientes.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmClientes")
                {

                    frmClientes Clientes = new frmClientes(labelOcupacion.Text, auxLogin, auxPass);
                    Clientes.MdiParent = this.MdiParent;
                    Clientes.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }

            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientesC;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = false;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void btnEmpleados_Click_1(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmEmpleados Empleados = new frmEmpleados(labelOcupacion.Text, auxLogin, auxPass);
                Empleados.MdiParent = this.MdiParent;
                Empleados.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmEmpleados")
                {

                    frmEmpleados Empleados = new frmEmpleados(labelOcupacion.Text, auxLogin, auxPass);
                    Empleados.MdiParent = this.MdiParent;
                    Empleados.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }
            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleadosC;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = false;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }


        private void btnVehiculos_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmVehiculos Vehiculos = new frmVehiculos();
                Vehiculos.MdiParent = this.MdiParent;
                Vehiculos.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmVehiculos")
                {
                    frmVehiculos Vehiculos = new frmVehiculos();
                    Vehiculos.MdiParent = this.MdiParent;
                    Vehiculos.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }
            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productosC;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = false;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void btnPedidos_Click_1(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmPedidos Pedidos = new frmPedidos();
                Pedidos.MdiParent = this.MdiParent;
                Pedidos.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmPedidos")
                {

                    frmPedidos Pedidos = new frmPedidos();
                    Pedidos.MdiParent = this.MdiParent;
                    Pedidos.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }

            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventasC;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = false;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void btnOfertas_Click_1(object sender, EventArgs e)
        {

            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmOfertas Ofertas = new frmOfertas();
                Ofertas.MdiParent = this.MdiParent;
                Ofertas.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmOfertas")
                {

                    frmOfertas Ofertas = new frmOfertas();
                    Ofertas.MdiParent = this.MdiParent;
                    Ofertas.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }
            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertasC;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = false;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Desea cerrar sesión?", "Cerrar Sesión", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (this.ParentForm.MdiChildren.Length == 2)
                {
                    this.ParentForm.MdiChildren[1].Close();
                }
                else
                {
                    this.ParentForm.MdiChildren[2].Close();
                    this.ParentForm.MdiChildren[1].Close();
                }

                frmLogin fLog = new frmLogin();

                fLog.MdiParent = this.ParentForm;

                //Cierra el formulario actual
                this.Close();
                fLog.Show();
            }
        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Desea salir de la aplicación?", "Salir", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void cerrarSesiónToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Desea cerrar sesión?", "Cerrar Sesión", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (this.ParentForm.MdiChildren.Length == 2)
                {
                    //this.ParentForm.MdiChildren[2].Close();
                    this.ParentForm.MdiChildren[1].Close();
                }
                else
                {
                    this.ParentForm.MdiChildren[2].Close();
                    this.ParentForm.MdiChildren[1].Close();
                }

                frmLogin fLog = new frmLogin();

                fLog.MdiParent = this.ParentForm;

                //Cierra el formulario actual
                this.Close();
                fLog.Show();
            }
        }

        private void imprimirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmClientes")
            {
                frmClientes Clientes = new frmClientes();

                ENClientes tabla = new ENClientes();
                DataTable dtClientes = tabla.BuscarClientes();
                Clientes.dgvClientes.DataSource = dtClientes;

                frmImprimir Imprimir = new frmImprimir(Clientes.dgvClientes, "CLIENTES");
                Imprimir.Show();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmEmpleados")
            {
                frmEmpleados Empleados = new frmEmpleados();

                ENEmpleados tabla = new ENEmpleados();
                DataTable dtEmpleados = tabla.BuscarEmpleados();
                Empleados.dgvEmpleados.DataSource = dtEmpleados;

                frmImprimir Imprimir = new frmImprimir(Empleados.dgvEmpleados, "EMPLEADOS");
                Imprimir.Show();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmVehiculos")
            {
                frmVehiculos Vehiculos = new frmVehiculos();

                ENProductos tabla = new ENProductos();
                DataTable dtVehiculos = tabla.BuscarVehiculo();
                Vehiculos.dgvVehiculos.DataSource = dtVehiculos;

                frmImprimir Imprimir = new frmImprimir(Vehiculos.dgvVehiculos, "VEHICULOS");
                Imprimir.Show();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmPedidos")
            {
                frmPedidos fpedidos = (frmPedidos)this.MdiParent.MdiChildren[2];

                fpedidos.ImprimirTabla();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmOfertas")
            {
                frmOfertas Ofertas = new frmOfertas();

                ENOfertas tabla = new ENOfertas();
                DataTable dtOfertas = tabla.BuscarOfertas();
                Ofertas.dvgofertas.DataSource = dtOfertas;

                frmImprimir Imprimir = new frmImprimir(Ofertas.dvgofertas, "OFERTAS");
                Imprimir.Show();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmCitas")
            {
                frmCitas fcitas = (frmCitas)this.MdiParent.MdiChildren[2];

                //fcitas.ImprimirTabla();
            }
        }

        private void clientesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmClientes Clientes = new frmClientes(labelOcupacion.Text, auxLogin, auxPass);
                Clientes.MdiParent = this.MdiParent;
                Clientes.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmClientes")
                {

                    frmClientes Clientes = new frmClientes(labelOcupacion.Text, auxLogin, auxPass);
                    Clientes.MdiParent = this.MdiParent;
                    Clientes.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }
            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientesC;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = false;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void empleadosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmEmpleados Empleados = new frmEmpleados(labelOcupacion.Text, auxLogin, auxPass);
                Empleados.MdiParent = this.MdiParent;
                Empleados.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmEmpleados")
                {

                    frmEmpleados Empleados = new frmEmpleados(labelOcupacion.Text, auxLogin, auxPass);
                    Empleados.MdiParent = this.MdiParent;
                    Empleados.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }
            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleadosC;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = false;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void vehículosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmVehiculos Vehiculos = new frmVehiculos();
                Vehiculos.MdiParent = this.MdiParent;
                Vehiculos.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmVehiculos")
                {
                    frmVehiculos Vehiculos = new frmVehiculos();
                    Vehiculos.MdiParent = this.MdiParent;
                    Vehiculos.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }
            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productosC;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = false;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void ventasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmPedidos Pedidos = new frmPedidos();
                Pedidos.MdiParent = this.MdiParent;
                Pedidos.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmPedidos")
                {

                    frmPedidos Pedidos = new frmPedidos();
                    Pedidos.MdiParent = this.MdiParent;
                    Pedidos.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }

            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventasC;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = false;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void ofertasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                frmOfertas Ofertas = new frmOfertas();
                Ofertas.MdiParent = this.MdiParent;
                Ofertas.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmOfertas")
                {

                    frmOfertas Ofertas = new frmOfertas();
                    Ofertas.MdiParent = this.MdiParent;
                    Ofertas.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }
            btnCitas.Image = uacars.Properties.Resources.citas;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertasC;

            citasToolStripMenuItem.Enabled = true;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = false;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void citasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren.Length == 2)
            {
                DataTable tmp = new DataTable();
                frmCitas Citas = new frmCitas(tmp);
                Citas.MdiParent = this.MdiParent;
                Citas.Show();
            }
            else
            {
                if (this.ParentForm.MdiChildren[2].Name.ToString() != "frmCitas")
                {
                    DataTable tmp = new DataTable();
                    frmCitas Citas = new frmCitas(tmp);
                    Citas.MdiParent = this.MdiParent;
                    Citas.Show();
                    this.ParentForm.MdiChildren[2].Close();
                }
            }

            btnCitas.Image = uacars.Properties.Resources.citasC;
            btnClientes.Image = uacars.Properties.Resources.clientes;
            btnEmpleados.Image = uacars.Properties.Resources.empleados;
            btnVehiculos.Image = uacars.Properties.Resources.productos;
            btnPedidos.Image = uacars.Properties.Resources.ventas;
            btnOfertas.Image = uacars.Properties.Resources.ofertas;

            citasToolStripMenuItem.Enabled = false;
            clientesToolStripMenuItem.Enabled = true;
            empleadosToolStripMenuItem.Enabled = true;
            vehículosToolStripMenuItem.Enabled = true;
            ventasToolStripMenuItem.Enabled = true;
            ofertasToolStripMenuItem.Enabled = true;

            imprimirToolStripMenuItem.Enabled = true;
            importarToolStripMenuItem.Enabled = true;
            exportarToolStripMenuItem.Enabled = true;
            guardarBDToolStripMenuItem.Enabled = true;
            restaurarBDToolStripMenuItem.Enabled = true;
            limpiarCamposToolStripMenuItem.Enabled = true;
        }

        private void acerdaDeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAcercaDe form1 = new frmAcercaDe();

            form1.Show();
        }

        private void frmMenuSuperior_Load(object sender, EventArgs e)
        {
            imprimirToolStripMenuItem.Enabled = false;
            importarToolStripMenuItem.Enabled = false;
            exportarToolStripMenuItem.Enabled = false;
            guardarBDToolStripMenuItem.Enabled = false;
            restaurarBDToolStripMenuItem.Enabled = false;
            limpiarCamposToolStripMenuItem.Enabled = false;
        }

        private void limpiarCamposToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmClientes")
            {
                frmClientes fclientes = (frmClientes)this.MdiParent.MdiChildren[2];

                fclientes.LimpiarCampos();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmCitas")
            {
                frmCitas fcitas = (frmCitas)this.MdiParent.MdiChildren[2];

                fcitas.LimpiarCampos();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmEmpleados")
            {
                frmEmpleados fempleados = (frmEmpleados)this.MdiParent.MdiChildren[2];

                fempleados.LimpiarCampos();
            }
            
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmVehiculos")
            {
                frmVehiculos fvehiculos = (frmVehiculos)this.MdiParent.MdiChildren[2];

                fvehiculos.LimpiarCampos();
            }
            
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmPedidos")
            {
                frmPedidos fpedidos = (frmPedidos)this.MdiParent.MdiChildren[2];

                fpedidos.LimpiarCampos();
            }
            
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmOfertas")
            {
                frmOfertas fofertas = (frmOfertas)this.MdiParent.MdiChildren[2];

                fofertas.LimpiarCampos();
            }
        }

        private void generarPDFform(string f)
        {
                Document pdfDocument = new Document();
                pdfDocument.SetPageSize(PageSize.A4);
                //pdfDocument.SetMargins(5, 5, 5, 5);

                //PdfPCell cell;
                string texto="";
                //float cabeceras = {};

                Chunk boo = new Chunk("UACARS 2008-2009");
                boo.Font.SetStyle("bold");
                boo.Font.Size = 30;
                //pdfDocument.Add(boo);

                if (f== "frmCitas")
                {
                    saveFileDialog1.FileName = "UACARS citas.pdf";
                    Paragraph p2 = new Paragraph("Documento generado de citas");
                    saveFileDialog1.Filter = "Archivo pdf|*.pdf";
                    saveFileDialog1.ShowDialog();

                    if (saveFileDialog1.FileName == "")
                        return;

                    PdfWriter.GetInstance(pdfDocument, new FileStream(saveFileDialog1.FileName, FileMode.Create));
                    pdfDocument.Open();
                    pdfDocument.Add(boo);
                    p2.Alignment = 1;
                    pdfDocument.Add(p2);
                    texto = "Citas Encontrados";
                }
                if (f== "frmClientes")
                {
                    pdfDocument.SetPageSize(PageSize.A4.Rotate());
                    saveFileDialog1.FileName = "UACARS clientes.pdf";
                    Paragraph p2 = new Paragraph("Documento generado de clientes");
                    saveFileDialog1.Filter = "Archivo pdf|*.pdf";
                    saveFileDialog1.ShowDialog();

                    if (saveFileDialog1.FileName == "")
                        return;

                    PdfWriter.GetInstance(pdfDocument, new FileStream(saveFileDialog1.FileName, FileMode.Create));
                    pdfDocument.Open();
                    pdfDocument.Add(boo);
                    p2.Alignment = 1;
                    pdfDocument.Add(p2);
                    texto="Clientes Encontrados";
                }
                if (f== "frmEmpleados")
                {
                    pdfDocument.SetPageSize(PageSize.A4.Rotate());
                    saveFileDialog1.FileName = "UACARS empleados.pdf";
                    Paragraph p2 = new Paragraph("Documento generado de empleados");
                    saveFileDialog1.Filter = "Archivo pdf|*.pdf";
                    saveFileDialog1.ShowDialog();

                    if (saveFileDialog1.FileName == "")
                        return;

                    PdfWriter.GetInstance(pdfDocument, new FileStream(saveFileDialog1.FileName, FileMode.Create));
                    pdfDocument.Open();
                    pdfDocument.Add(boo);
                    p2.Alignment = 1;
                    pdfDocument.Add(p2);
                    texto = "Empleados Encontrados";
                }
                if (f== "frmOfertas")
                {
                    pdfDocument.SetPageSize(PageSize.A4.Rotate());
                    saveFileDialog1.FileName = "UACARS ofertas.pdf";
                    Paragraph p2 = new Paragraph("Documento generado de ofertas");
                    saveFileDialog1.Filter = "Archivo pdf|*.pdf";
                    saveFileDialog1.ShowDialog();

                    if (saveFileDialog1.FileName == "")
                        return;

                    PdfWriter.GetInstance(pdfDocument, new FileStream(saveFileDialog1.FileName, FileMode.Create));
                    pdfDocument.Open();
                    pdfDocument.Add(boo);
                    p2.Alignment = 1;
                    pdfDocument.Add(p2);
                    texto = "Ofertas Encontradas";
                }
                if (f== "frmPedidos")
                {
                    pdfDocument.SetPageSize(PageSize.A4.Rotate());
                    saveFileDialog1.FileName = "UACARS pedidos.pdf";
                    Paragraph p2 = new Paragraph("Documento generado de pedidos");
                    saveFileDialog1.Filter = "Archivo pdf|*.pdf";
                    saveFileDialog1.ShowDialog();

                    if (saveFileDialog1.FileName == "")
                        return;

                    PdfWriter.GetInstance(pdfDocument, new FileStream(saveFileDialog1.FileName, FileMode.Create));
                    pdfDocument.Open();
                    pdfDocument.Add(boo);
                    p2.Alignment = 1;
                    pdfDocument.Add(p2);
                    texto = "Pedidos Encontrados";
                }
                if (f== "frmVehiculos")
                {
                    pdfDocument.SetPageSize(PageSize.A4.Rotate());
                    saveFileDialog1.FileName = "UACARS vehiculos.pdf";
                    Paragraph p2 = new Paragraph("Documento generado de vehiculos");
                    saveFileDialog1.Filter = "Archivo pdf|*.pdf";
                    saveFileDialog1.ShowDialog();

                    if (saveFileDialog1.FileName == "")
                        return;

                    PdfWriter.GetInstance(pdfDocument, new FileStream(saveFileDialog1.FileName, FileMode.Create));
                    pdfDocument.Open();
                    pdfDocument.Add(boo);
                    p2.Alignment = 1;
                    pdfDocument.Add(p2);
                    texto = "Vehiculos Encontrados";
                }

                //saveFileDialog1.Filter = "Archivo pdf|*.pdf";
                //saveFileDialog1.ShowDialog();

                //if (saveFileDialog1.FileName == "")
                    //return;


                //PdfWriter.GetInstance(pdfDocument, new FileStream(saveFileDialog1.FileName, FileMode.Create));
                //pdfDocument.Open();

                


                //Paragraph p2 = new Paragraph("Documento generado de citas");

                //p2.Alignment = 1;
                //pdfDocument.Add(p2);


                //if (this.MdiParent.MdiChildren[2].WindowState == FormWindowState.Normal)
                //MessageBox.Show("YES");
                //int num = frasd.dataGridView1.Rows.Count;


                int numFilas = 0;
                int numColumnas = 0;

                frmCitas fc1 = null;
                frmClientes fc2 = null;
                frmEmpleados fc3 = null;
                frmOfertas fc4 = null;
                frmVehiculos fc5 = null;
                frmPedidos fc6 = null;

                if (f == "frmCitas")
                {
                    fc1 = (frmCitas)this.MdiParent.MdiChildren[2];
                    numFilas = fc1.dataGridView1.Rows.Count;
                    numColumnas = fc1.dataGridView1.ColumnCount;
                }
                if (f == "frmClientes")
                {
                    fc2 = (frmClientes)this.MdiParent.MdiChildren[2];
                    numFilas = fc2.dgvClientes.Rows.Count;
                    numColumnas = fc2.dgvClientes.ColumnCount;
                }
                if (f == "frmEmpleados")
                {
                    fc3 = (frmEmpleados)this.MdiParent.MdiChildren[2];
                    numFilas = fc3.dgvEmpleados.Rows.Count;
                    numColumnas = fc3.dgvEmpleados.ColumnCount;
                }
                if (f == "frmOfertas")
                {
                    fc4 = (frmOfertas)this.MdiParent.MdiChildren[2];
                    numFilas = fc4.dvgofertas.Rows.Count;
                    numColumnas = fc4.dvgofertas.ColumnCount;
                }
                if (f == "frmVehiculos")
                {
                    fc5 = (frmVehiculos)this.MdiParent.MdiChildren[2];
                    numFilas = fc5.dgvVehiculos.Rows.Count;
                    numColumnas = fc5.dgvVehiculos.ColumnCount;
                }
                if (f == "frmPedidos")
                {
                    fc6 = (frmPedidos)this.MdiParent.MdiChildren[2];
                    numFilas = fc6.dgvPedidos.Rows.Count;
                    numColumnas = fc6.dgvPedidos.ColumnCount;
                }


                //int numFilas = fc.dataGridView1.Rows.Count;
                //int numColumnas = fc.dataGridView1.ColumnCount;

                if (numColumnas == 0)
                    return;

                PdfPTable table = new PdfPTable(numColumnas);
                table.HorizontalAlignment = 1;
                table.SpacingBefore = 20f;
                table.SpacingAfter = 30f;
                PdfPCell cell = new PdfPCell(new Phrase(texto));


                //float[] headerwidths = { 18, 14, 14, 18, 18, 14 };
                //table.SetWidths(headerwidths);

                table.WidthPercentage = 100;
                cell.HorizontalAlignment = 1;
                cell.Colspan = numColumnas;
                cell.Border = 1;
                cell.HorizontalAlignment = 0;

                table.AddCell(cell);
                pdfDocument.Add(cell);

                if (f== "frmCitas")
                {
                    table.AddCell("ID CITA");
                    table.AddCell("FECHA");
                    table.AddCell("HORA");
                    table.AddCell("CLIENTE");
                    table.AddCell("EMPLEADO");
                    table.AddCell("MOTIVO");
                }

                if (f== "frmClientes")
                {
                    table.AddCell("ID CLIENTE");
                    table.AddCell("NIF");
                    table.AddCell("NOMBRE");
                    table.AddCell("APELLIDO1");
                    table.AddCell("APELLIDO2");
                    table.AddCell("TELEFONO");
                    table.AddCell("EMAIL");
                    table.AddCell("DIRECCION");
                    table.AddCell("LOCALIDAD");
                    table.AddCell("PROVINCIA");
                    table.AddCell("CP");
                    table.AddCell("ALTA");
                    table.AddCell("LOGIN");
                    table.AddCell("PASS");
                }

                if (f == "frmEmpleados")
                {
                    table.AddCell("ID");
                    table.AddCell("NIF");
                    table.AddCell("NOMBRE");
                    table.AddCell("APELLIDO1");
                    table.AddCell("APELLIDO2");
                    table.AddCell("TELEFONO");
                    table.AddCell("EMAIL");
                    table.AddCell("DIRECCION");
                    table.AddCell("LOCALIDAD");
                    table.AddCell("PROVINCIA");
                    table.AddCell("CP");
                    table.AddCell("CARGO");
                    table.AddCell("SUELDO");
                    table.AddCell("ALTA");
                    table.AddCell("LOGIN");
                    table.AddCell("PASS");
                }
                if (f == "frmOfertas")
                {
                    table.AddCell("ID");
                    table.AddCell("CLIENTE");
                    table.AddCell("VEHICULO");
                    table.AddCell("CHOFER");
                    table.AddCell("INICIO");
                    table.AddCell("FIN");
                    table.AddCell("%");
                    table.AddCell("Dias GRATIS");
                    table.AddCell("DESCRIPCION");
                }
                if (f == "frmVehiculos")
                {
                    table.AddCell("ID");
                    table.AddCell("MARCA");
                    table.AddCell("MODELO");
                    table.AddCell("MATRICULA");
                    table.AddCell("COLOR");
                    table.AddCell("PAP");
                    table.AddCell("PVP");
                    table.AddCell("CATEGORIA");
                    table.AddCell("URL");
                }
                if (numFilas > 0)
                {
                    for (int i = 0; i < numFilas; i++)
                    {

                        if (i % 2 == 1)
                        {
                            table.DefaultCell.GrayFill = 0.6f;
                        }
                        else
                            table.DefaultCell.GrayFill = 0.2f;

                        for (int j = 0; j < numColumnas; j++)
                        { 
                            /*
                            if (j == 1)
                            {
                                string[] trozos = fc.dataGridView1[j, i].Value.ToString().Split(' ');
                                string trozo1 = trozos[0].Trim();
                                table.AddCell(trozo1);
                            }
                            else
                            {
                                if (j == 2)
                                {
                                    string[] trozos = fc.dataGridView1[j, i].Value.ToString().Split(' ');
                                    string trozo1 = trozos[1].Trim();
                                    table.AddCell(trozo1);
                                }
                                else
                                {
                             */ 
                                    if(f=="frmCitas")
                                        table.AddCell(fc1.dataGridView1[j, i].Value.ToString());
                                    if(f=="frmClientes")
                                        table.AddCell(fc2.dgvClientes[j, i].Value.ToString());
                                    if (f == "frmEmpleados")
                                        table.AddCell(fc3.dgvEmpleados[j, i].Value.ToString());
                                    if (f == "frmOfertas")
                                        table.AddCell(fc4.dvgofertas[j, i].Value.ToString());
                                    if (f == "frmVehiculos")
                                        table.AddCell(fc5.dgvVehiculos[j, i].Value.ToString());
                                    if (f == "frmCPedidos")
                                        table.AddCell(fc6.dgvPedidos[j, i].Value.ToString());
                        }
                    }
                    pdfDocument.Add(table);
                }

                pdfDocument.Close();
        }

        private void pDFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            generarPDFform(this.MdiParent.MdiChildren[2].Name.ToString());

        }

        private void guardarBDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.FileName = "uacarsBDbackcup.bak";
            saveFileDialog1.Filter = "Archivo BAK|*.bak";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = saveFileDialog1.FileName;

                string sBackup = "BACKUP DATABASE " + "uacarsBD" +
                     " TO DISK = N'" + path +
                     "' WITH NOFORMAT, NOINIT, NAME =N'" + "uacarsBD" +
                     "-Full Database Backup',SKIP, STATS = 10";

                SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();

                csb.DataSource = ".\\SQLEXPRESS";
                csb.InitialCatalog = "uacarsBD";
                csb.IntegratedSecurity = true;

                using (SqlConnection con = new SqlConnection(csb.ConnectionString))
                {
                    try
                    {
                        con.Open();

                        SqlCommand cmdBackUp = new SqlCommand(sBackup, con);

                        cmdBackUp.ExecuteNonQuery();

                        MessageBox.Show("Se ha creado un BackUp de La base de datos satisfactoriamente",
                            "Copia de seguridad de base de datos",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);

                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message,
                            "Error al copiar la base de datos",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void restaurarBDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Archivo BAK|*.bak";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string file = openFileDialog1.FileName;

                if (DialogResult.Cancel == MessageBox.Show("ATENCIÓN.CON ESTA ACCIÓN BORRARA EL CONTENIDO ACTUAL DE SU BD", "ATENCION", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2))
                    return;

                string sRestore = "restore database StreamlineDB" + "FROM DISK=" +
                    file + "WITH recovery, REPLACE";

                SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();

                csb.DataSource = ".\\SQLEXPRESS";
                csb.InitialCatalog = "uacarsBD";
                csb.IntegratedSecurity = true;

                using (SqlConnection con = new SqlConnection(csb.ConnectionString))
                {
                    try
                    {
                        con.Open();

                        SqlCommand cmdRestore = new SqlCommand(sRestore, con);

                        cmdRestore.ExecuteNonQuery();

                        MessageBox.Show("Se ha creado un BackUp de La base de datos satisfactoriamente",
                            "Copia de seguridad de base de datos",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);

                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message,
                            "Error al copiar la base de datos",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void xMLToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmClientes")
            {
                frmClientes fclientes = (frmClientes)this.MdiParent.MdiChildren[2];

                fclientes.ExportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmCitas")
            {
                frmCitas fcitas = (frmCitas)this.MdiParent.MdiChildren[2];

                fcitas.ExportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmEmpleados")
            {
                frmEmpleados fempleados = (frmEmpleados)this.MdiParent.MdiChildren[2];

                fempleados.ExportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmVehiculos")
            {
                frmVehiculos fvehiculos = (frmVehiculos)this.MdiParent.MdiChildren[2];

                fvehiculos.ExportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmPedidos")
            {
                frmPedidos fpedidos = (frmPedidos)this.MdiParent.MdiChildren[2];

                fpedidos.ExportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmOfertas")
            {
                frmOfertas fofertas = (frmOfertas)this.MdiParent.MdiChildren[2];

                fofertas.ExportarXML();
            }
        }

        private void xMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmClientes")
            {
                frmClientes fclientes = (frmClientes)this.MdiParent.MdiChildren[2];

                fclientes.ImportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmCitas")
            {
                frmCitas fcitas = (frmCitas)this.MdiParent.MdiChildren[2];

                fcitas.ImportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmEmpleados")
            {
                frmEmpleados fempleados = (frmEmpleados)this.MdiParent.MdiChildren[2];

                fempleados.ImportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmVehiculos")
            {
                frmVehiculos fvehiculos = (frmVehiculos)this.MdiParent.MdiChildren[2];

                fvehiculos.ImportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmPedidos")
            {
                frmPedidos fpedidos = (frmPedidos)this.MdiParent.MdiChildren[2];

                fpedidos.ImportarXML();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmOfertas")
            {
                frmOfertas fofertas = (frmOfertas)this.MdiParent.MdiChildren[2];

                fofertas.ImportarXML();
            }
        }

        private void datosEstadísticosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmEstadisticas fe = new frmEstadisticas();
            fe.MdiParent = this.MdiParent;
            fe.Show() ;
        }

        private void excelToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmClientes")
            {
                frmClientes fclientes = (frmClientes)this.MdiParent.MdiChildren[2];

                fclientes.Excel();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmCitas")
            {
                frmCitas fcitas = (frmCitas)this.MdiParent.MdiChildren[2];

                fcitas.Excel();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmEmpleados")
            {
                frmEmpleados fempleados = (frmEmpleados)this.MdiParent.MdiChildren[2];

                fempleados.Excel();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmVehiculos")
            {
                frmVehiculos fvehiculos = (frmVehiculos)this.MdiParent.MdiChildren[2];

                fvehiculos.Excel();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmPedidos")
            {
                frmPedidos fpedidos = (frmPedidos)this.MdiParent.MdiChildren[2];

                fpedidos.Excel();
            }

            if (this.ParentForm.MdiChildren[2].Name.ToString() == "frmOfertas")
            {
                frmOfertas fofertas = (frmOfertas)this.MdiParent.MdiChildren[2];

                fofertas.Excel();
            }
        }
    }
}
